// pages/Ranking/Ranking.js
const app = getApp()
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      userInfo:{},
      hasUserInfo: false,
      currentTab:0,
      FriendsRank:0,
      WorldRank:0,
      WorldRankings:{}
  },
    RankTypeTap: function (e) {
        var that = this;
        var newId = e.currentTarget.dataset.id;
        if (this.data.currentTab == newId) {
            return false;
        } else {
            that.setData({
                currentTab: newId
            })
        }
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if(app.globalData.userInfo){
      that.setData({
          userInfo:app.globalData.userInfo,
          hasUserInfo:true,
      })
    }
    that.getAll_rank()
    that.rankList()
      /*if(app.globalData.group_id){
          that.rankList(app.globalData.group_id)
      }*/
  },
    getAll_rank:function () {
        var that = this;
        wx.showLoading({
            title: '加载中...',
        })
        wx.request({
            url: 'https://caige1.sanliwenhua.com/program/rank/pass',
            method: "get",
            data:{
                x_token:app.globalData.userInfo?app.globalData.userInfo.token:"",
                app:7
            },
            success: function (res) {
                console.log(res)
                if (res.data.code == 200) {
                    wx.hideLoading()
                    console.log(res.data)
                    /*that.setData({
                        WorldRank: res.data.data.my
                    })*/
                    var data = res.data.data.data;
                    console.log(data)
                    var WorldRankings = [];
                    for (var i = 0; i < data.length; i++) {
                        var WorldRankingsData = {}
                        WorldRankingsData.ranking = i + 1;
                        WorldRankingsData.userAvatar = data[i].user.avatar;
                        WorldRankingsData.userName = data[i].user.nickname;
                        WorldRankingsData.ShutDown = data[i].pass;
                        WorldRankings.push(WorldRankingsData)
                    }
                    that.setData({
                        WorldRankings: WorldRankings
                    })
                    console.log(WorldRankings)

                } else {
                    wx.showToast({
                        title: res.data.message,
                        image: '/image/errIcon.png',
                        duration: 2000
                    })

                }
            },
            fail: function (res) {
                wx.showToast({
                    title: res.data.message,
                    image: '/image/errIcon.png',
                    duration: 2000
                })
            }
        })
    },
    rankList: function (a) {
        var that = this;
        //console.log(that.data.token)
        //console.log('caige1.sanliwenhua.com/rank',that.data.group_id, app.globalData.group_id, 'token', that.data.token, 'token_g', app.globalData.userInfo.token)
        wx.request({
            url: 'https://caige1.sanliwenhua.com/program/rank/frequency?app=7',
            method: "get",
            data:{
                x_token:app.globalData.userInfo?app.globalData.userInfo.token:""
            },
            success: function (res) {
                console.log('用戶獲取自己群內的信息', res)
                if (res.data.code == 200) {
                    var data = res.data.data.data;
                    console.log(data)
                    var FriendsRankings = [];
                    for (var i = 0; i < data.length; i++) {
                        var FriendsRankingsData = {}
                        FriendsRankingsData.ranking = i + 1;
                        FriendsRankingsData.userAvatar = data[i].user.avatar;
                        FriendsRankingsData.userName = data[i].user.nickname;
                        FriendsRankingsData.ShutDown = data[i].frequency;
                        FriendsRankings.push(FriendsRankingsData)
                    }
                    that.setData({
                        //FriendsRank: res.data.data.my,
                        FriendsRankings: FriendsRankings
                    })
                    console.log(FriendsRankings)

                } else {

                }
            },
            fail: function (res) {

            }
        })


    },
    update:function () {
        var that = this
        var NewTime = new Date().getTime().toString();
        var key = util.sha1(app.globalData.userInfo.id + NewTime + app.globalData.userInfo.checkpoint + app.globalData.userInfo.id);
        key = key.toUpperCase()
        var data = {}
        console.log('update群场景shareTicket:',app.globalData)
        var update = function (data) {
            wx.request({
                url: 'https://caige1.sanliwenhua.com/idiom/update_checkpoint',
                method: "post",
                data: data,
                success: function (res) {
                    console.log(res)
                    app.globalData.group_id = res.data.group_id;
                },
                fail: function (res) {
                    console.log(res)
                    wx.showToast({
                        title: res.data.message,
                        icon: 'success',
                        duration: 2000
                    })
                }
            })
        }
        if(app.globalData.shareTicket){
            wx.getShareInfo({
                shareTicket:app.globalData.shareTicket,
                success:function (res) {
                    console.log(res)
                    app.globalData.Sharing= {}
                    app.globalData.Sharing.errMsg = res.errMsg
                    app.globalData.Sharing.encryptedData = res.encryptedData
                    app.globalData.Sharing.iv = res.iv
                    console.log(app.globalData.Sharing)
                    data = {
                        key: key,
                        time: NewTime,
                        checkpoint: app.globalData.userInfo.checkpoint,
                        x_token: app.globalData.userInfo.token,
                        errMsg: res.errMsg,
                        encryptedData: res.encryptedData,
                        iv: res.iv,
                        session_key: app.globalData.userInfo.session_key
                    }
                    update(data);
                },
                fail:function (res) {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'success',
                        duration: 2000
                    })
                }
            })
        }else{
            data = {
                key: key,
                time: NewTime,
                checkpoint: app.globalData.userInfo.checkpoint,
                x_token: app.globalData.userInfo.token
            }
            update(data);
        }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        wx.showShareMenu({
            withShareTicket: true
        })
        if(app.globalData.userInfo){
            this.update()
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        var that = this;
        var nickeName = that.data.userInfo.nickName
        if (res.from === 'button') {

        }
        return {
            title: '['+nickeName+'@我],那些年学过的成语，还记得吗？快来挑战吧！',
            path: 'pages/index/index',
            imageUrl: 'http://paz0eiqln.bkt.clouddn.com/image/fenxing2.jpg',
            success: function (res) {
                wx.showToast({
                    title: '分享成功!',
                    icon: 'success',
                    duration: 2000
                })
                console.log('分享到群的标记', res)
                /*if(res.shareTickets){
                    app.globalData.shareTicket = res.shareTickets[0]
                    if(app.globalData.platform == "android"){
                        this.update()
                    }
                    console.log(app.globalData.Sharing)
                }else{
                    wx.showModal({
                        title: "提示",
                        content: "分享到群可以看到朋友的排行哦!",
                        showCancel: false,
                        confirmText: "确定"
                    })

                }*/
            },
            fail: function (res) {
                console.log(res)
                wx.showToast({
                    title: '分享失败！',
                    image: '/image/errIcon.png',
                    duration: 2000
                })
            }
        }
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  }
})